home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrtools-1.10 / misc / io.c next >
Encoding:
C/C++ Source or Header  |  2000-05-07  |  5.1 KB  |  220 lines

  1. /* @(#)io.c    1.19 98/10/10 Copyright 1988 J. Schilling */
  2. #ifndef lint
  3. static    char sccsid[] =
  4.     "@(#)io.c    1.19 98/10/10 Copyright 1988 J. Schilling";
  5. #endif
  6. /*
  7.  *    Copyright (c) 1988 J. Schilling
  8.  */
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2, or (at your option)
  13.  * any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include <mconfig.h>
  26. #include <stdio.h>
  27. #include <standard.h>
  28. #include <ctype.h>
  29. #include <vadefs.h>
  30. #include <stdxlib.h>
  31. #include <strdefs.h>
  32. #include <utypes.h>
  33. #include <schily.h>
  34.  
  35. struct disk {
  36.     int    dummy;
  37. };
  38.  
  39. LOCAL    char    *skipwhite    __PR((const char *));
  40. LOCAL    void    prt_std        __PR((char *, long, long, long, struct disk *));
  41. EXPORT    BOOL    cvt_std        __PR((char *, long *, long, long, struct disk *));
  42. extern    BOOL    getvalue    __PR((char *, long *, long, long,
  43.                 void (*)(char *, long, long, long, struct disk *),
  44.                 BOOL (*)(char *, long *, long, long, struct disk *),
  45.                 struct disk *));
  46. extern    BOOL    getlong        __PR((char *, long *, long, long));
  47. extern    BOOL    getint        __PR((char *, int *, int, int));
  48. extern    BOOL    yes        __PR((char *, ...));
  49.  
  50. LOCAL
  51. char *skipwhite(s)
  52.          const char    *s;
  53. {
  54.     register const Uchar    *p = (const Uchar *)s;
  55.  
  56.     while (*p) {
  57.         if (!isspace(*p))
  58.             break;
  59.         p++;
  60.     }
  61.     return ((char *)p);
  62. }
  63.  
  64. /* ARGSUSED */
  65. EXPORT
  66. BOOL
  67. cvt_std(linep, lp, mini, maxi, dp)
  68.     char    *linep;
  69.     long    *lp;
  70.     long    mini;
  71.     long    maxi;
  72.     struct disk    *dp;
  73. {
  74.     long    l    = -1L;
  75.  
  76. /*    printf("cvt_std(\"%s\", %d, %d, %d);\n", linep, *lp, mini, maxi);*/
  77.  
  78.     if (linep[0] == '?') {
  79.         printf("Enter a number in the range from %ld to %ld\n",
  80.                                 mini, maxi);
  81.         printf("The default radix is 10\n");
  82.         printf("Precede number with '0x' for hexadecimal or with '0' for octal\n");
  83.         printf("Shorthands are:\n");
  84.         printf("\t'^' for minimum value (%ld)\n", mini);
  85.         printf("\t'$' for maximum value (%ld)\n", maxi);
  86.         printf("\t'+' for incrementing value to %ld\n", *lp + 1);
  87.         printf("\t'-' for decrementing value to %ld\n", *lp - 1);
  88.         return (FALSE);
  89.     }
  90.     if (linep[0] == '^' && *skipwhite(&linep[1]) == '\0') {
  91.         l = mini;
  92.     } else if (linep[0] == '$' && *skipwhite(&linep[1]) == '\0') {
  93.         l = maxi;
  94.     } else if (linep[0] == '+' && *skipwhite(&linep[1]) == '\0') {
  95.         if (*lp < maxi)
  96.             l = *lp + 1;
  97.     } else if (linep[0] == '-' && *skipwhite(&linep[1]) == '\0') {
  98.         if (*lp > mini)
  99.             l = *lp - 1;
  100.     } else if (*astol(linep, &l)) {
  101.         printf("Not a number: '%s'.\n", linep);
  102.         return (FALSE);
  103.     }
  104.     if (l < mini || l > maxi) {
  105.         printf("'%s' is out of range.\n", linep);
  106.         return (FALSE);
  107.     }
  108.     *lp = l;
  109.     return (TRUE);
  110. }
  111.  
  112. /* ARGSUSED */
  113. LOCAL void
  114. prt_std(s, l, mini, maxi, dp)
  115.     char    *s;
  116.     long    l;
  117.     long    mini;
  118.     long    maxi;
  119.     struct disk *dp;
  120. {
  121.     printf("%s %ld (%ld - %ld)/<cr>:", s, l, mini, maxi);
  122. }
  123.  
  124. EXPORT
  125. BOOL getvalue(s, lp, mini, maxi, prt, cvt, dp)
  126.     char    *s;
  127.     long    *lp;
  128.     long    mini;
  129.     long    maxi;
  130.     void    (*prt) __PR((char *, long, long, long, struct disk *));
  131.     BOOL    (*cvt) __PR((char *, long *, long, long, struct disk *));
  132.     struct disk    *dp;
  133. {
  134.     char    line[128];
  135.     char    *linep;
  136.  
  137.     for(;;) {
  138.         (*prt)(s, *lp, mini, maxi, dp);
  139.         flush();
  140.         line[0] = '\0';
  141.         if (getline(line, 80) == EOF)
  142.             exit(EX_BAD);
  143.  
  144.         linep = skipwhite(line);
  145.         /*
  146.          * Nicht initialisierte Variablen
  147.          * duerfen nicht uebernommen werden
  148.          */
  149.         if (linep[0] == '\0' && *lp != -1L)
  150.             return (FALSE);
  151.  
  152.         if (strlen(linep) == 0) {
  153.             /* Leere Eingabe */
  154.         } else if ((*cvt)(linep, lp, mini, maxi, dp))
  155.             return (TRUE);
  156.     }
  157.     /* NOTREACHED */
  158. }
  159.  
  160. EXPORT
  161. BOOL getlong(s, lp, mini, maxi)
  162.     char    *s;
  163.     long    *lp;
  164.     long    mini;
  165.     long    maxi;
  166. {
  167.     return (getvalue(s, lp, mini, maxi, prt_std, cvt_std, (void *)0));
  168. }
  169.  
  170. EXPORT
  171. BOOL getint(s, ip, mini, maxi)
  172.     char    *s;
  173.     int    *ip;
  174.     int    mini;
  175.     int    maxi;
  176. {
  177.     long    l = *ip;
  178.     BOOL    ret;
  179.  
  180.     ret = getlong(s, &l, (long)mini, (long)maxi);
  181.     *ip = l;
  182.     return (ret);
  183. }
  184.  
  185. /* VARARGS1 */
  186. #ifdef    PROTOTYPES
  187. EXPORT BOOL yes(char *form, ...)
  188. #else
  189. EXPORT
  190. BOOL yes(form, va_alist)
  191.     char    *form;
  192.     va_dcl
  193. #endif
  194. {
  195.     va_list    args;
  196.     char okbuf[10];
  197.  
  198. again:
  199. #ifdef    PROTOTYPES
  200.     va_start(args, form);
  201. #else
  202.     va_start(args);
  203. #endif
  204.     printf("%r", form, args);
  205.     va_end(args);
  206.     flush();
  207.     if (getline(okbuf, sizeof(okbuf)) == EOF)
  208.         exit(EX_BAD);
  209.     if (okbuf[0] == '?') {
  210.         printf("Enter 'y', 'Y', 'yes' or 'YES' if you agree with the previous asked question.\n");
  211.         printf("All other input will be handled as if the question has beed answered with 'no'.\n");
  212.         goto again;
  213.     }
  214.     if(streql(okbuf, "y") || streql(okbuf, "yes") ||
  215.        streql(okbuf, "Y") || streql(okbuf, "YES"))
  216.         return(TRUE);
  217.     else
  218.         return(FALSE);
  219. }
  220.